// 917. 仅仅反转字母
class Solution {
public:
    string reverseOnlyLetters(string s) {
        int n = s.size();
        int l = 0, r = n-1;
        while(l < r)
        {
            if(isalpha(s[l]) && isalpha(s[r]))
            {
                swap(s[l], s[r]);
                l++;
                r--;
            }
            if(!isalpha(s[l])) l++;
            if(!isalpha(s[r])) r--;
        }
        return s;
    }
};

// LCR 017. 最小覆盖子串
class Solution {
public:
    string minWindow(string s, string t) {
        // map1: 统计t中的字符频次  map2: 统计窗口中的字符频次
        char map1[128] = {0};
        char map2[128] = {0};
        int kinds = 0;
        for(char e : t) 
            if(map1[e]++ == 0) kinds++;

        int begin = -1, minlen = INT_MAX;
        for(int left = 0, right = 0, k_count = 0; right < s.size(); right++)
        {
            char in = s[right];
            if(++map2[in] == map1[in]) k_count++;

            while(kinds == k_count)
            {
                if(right - left + 1 < minlen)
                {
                    minlen = right - left + 1;
                    begin = left;
                }
                char out = s[left++];
                if(map2[out]-- == map1[out]) k_count--;
            }
        }
        if(begin == -1) return "";
        return s.substr(begin, minlen);
    }
};